package com.cyyang.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.cyyang.bean.Cart;
import com.cyyang.bean.CartVo;
import com.cyyang.bean.HgUser;
import com.cyyang.mapper.UserMapper;
import com.cyyang.service.UserService;

@Service(timeout = 10000)
public class UserServiceImpl implements UserService {

	@Autowired
	UserMapper userMapper;
	@Override
	public HgUser login(HgUser user) {
		return userMapper.login(user);
	}
	@Override
	public void addCart(CartVo vo) {
		Cart dbCart=userMapper.findCartByUidAndSkuId(vo);
		if(dbCart!=null) {
			/**
			 * 合并更新购物车
			 * 必须skuid和uid一样的情况下再合并
			 */
			int newPnum = vo.getPnum();
			BigDecimal oldTotal = dbCart.getSumTotal();
			BigDecimal price = vo.getPrice();
			//新的数量*价格+老的小计=新的小计
			BigDecimal newTotal = price.multiply(new BigDecimal(newPnum+"")).add(oldTotal);
			vo.setSumTotal(newTotal);
			userMapper.updateCart(vo);
		}else {
			//计算小计=单价*数量
			BigDecimal price = vo.getPrice();
			int pnum = vo.getPnum();
			BigDecimal smallTotal = price.multiply(new BigDecimal(pnum+""));
			vo.setSumTotal(smallTotal);
			vo.setCreatetime(new Date());
			vo.setUpdatetime(new Date());
			userMapper.addCart(vo);
		}
		
	}
	@Override
	public List<Cart> findCartListByUid(HgUser sessionUser) {
		return userMapper.findCartListByUid(sessionUser);
	}

}
